package logic

import (
	"autotestservice/testservices/internal/data"
	"autotestservice/testservices/internal/deviceMng"
	"autotestservice/testservices/internal/svc"
	"autotestservice/testservices/internal/types"
	"context"
	"gitee.com/jmdvirus/jkgo/jk/jklog"

	"github.com/tal-tech/go-zero/core/logx"
)

type AutotestItemTestManualDoneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAutotestItemTestManualDoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) AutotestItemTestManualDoneLogic {
	return AutotestItemTestManualDoneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutotestItemTestManualDoneLogic) AutotestItemTestManualDone(req types.TestProjectItem) (*types.TestProjectItemResponse, error) {
	jklog.L().Debugln("test manual done")
	tpi := req
	res := types.TestProjectItemResponse{
		Code:    0,
		Message: "success",
		Result:  tpi,
	}

	devOpt := deviceMng.NewDeviceOption(req.TestTarget.ConnectStyle, req.TestTarget.Addr, req.TestTarget.Port)
	findDev := deviceMng.GlobalDeviceMng().Find(*devOpt)

	if findDev != nil {
		dbreq := data.DBTestProjectItem{
			Id:      req.Id,
			Result:  req.Result,
			Content: req.Content,
		}
		ltpi, err := data.AutoTestTestProjectItemManualUpdateResult(dbreq)
		if err != nil {
			jklog.L().Errorln(err)
			res.Code = 505
			res.Message = "set fail"
			return &res, nil
		}
		res.Result.TimeEnd = ltpi.TimeEnd
	} else {
		res.Code = 1
		res.Message = "not connect"
	}

	return &res, nil
}
